package com.yunwaikeji.gy_tool_library.adapter;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.yunwaikeji.gy_tool_library.R;
import com.yunwaikeji.gy_tool_library.adapter.recycler.GYRecyclerAdapter;
import com.yunwaikeji.gy_tool_library.adapter.recycler.GYViewHolder;
import com.yunwaikeji.gy_tool_library.info.GYImageListViewInfo;
import com.yunwaikeji.gy_tool_library.utils.GYUtils;
import com.yunwaikeji.gy_tool_library.model.GYImageListModel;
import com.yunwaikeji.gy_tool_library.utils.picture.download.GYPictureDownload;
import com.yunwaikeji.gy_tool_library.view.GYImageView;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

public class GYImageAdapter extends GYRecyclerAdapter<GYImageListModel, GYViewHolder> implements GYImageListViewInfo {
    private Drawable addDrawable;
    private Drawable deleteDrawable;
    private int deleteWidthPx = WRAP_CONTENT;
    private int deleteHeightPx = WRAP_CONTENT;
    private int deleteDrawableMarginTopPx = 0;
    private int deleteDrawableMarginRightPx = 0;
    private int itemHeightPx = WRAP_CONTENT;
    private String itemProportion;

    public GYImageAdapter setDeleteDrawableMarginTopPx(int deleteDrawableMarginTopPx) {
        this.deleteDrawableMarginTopPx = deleteDrawableMarginTopPx;
        return this;
    }

    public GYImageAdapter setDeleteDrawableMarginRightPx(int deleteDrawableMarginRightPx) {
        this.deleteDrawableMarginRightPx = deleteDrawableMarginRightPx;
        return this;
    }

    public GYImageAdapter setAddDrawable(Drawable addDrawable) {
        this.addDrawable = addDrawable;
        return this;
    }

    public GYImageAdapter setDeleteDrawable(Drawable deleteDrawable) {
        this.deleteDrawable = deleteDrawable;
        return this;
    }

    public GYImageAdapter setDeleteWidthPx(int deleteWidthPx) {
        this.deleteWidthPx = deleteWidthPx;
        return this;
    }

    public GYImageAdapter setDeleteHeightPx(int deleteHeightPx) {
        this.deleteHeightPx = deleteHeightPx;
        return this;
    }

    public int getItemHeightPx() {
        return itemHeightPx;
    }

    public GYImageAdapter setItemHeightPx(int itemHeightPx) {
        this.itemHeightPx = itemHeightPx;
        return this;
    }

    public void setItemProportion(String itemProportion) {
        this.itemProportion = itemProportion;
    }

    public GYImageAdapter() {
        super(R.layout.adapter_gy_image);
        addChildClickViewIds(R.id.delImg);
    }

    @Override
    protected void convert(GYViewHolder helper, GYImageListModel item) {
        ImageView imageView = helper.getView(R.id.gyImg);
        GYImageView deleteImg = helper.getView(R.id.delImg);
        if (itemHeightPx == WRAP_CONTENT) {
            if (!TextUtils.isEmpty(itemProportion) && itemProportion.contains(":")) {
                String[] strings = itemProportion.split(":");
                if (strings.length == 2) {
                    if (GYUtils.isNumber(strings[0]) && GYUtils.isNumber(strings[1])) {
                        ConstraintSet constraintSet = new ConstraintSet();
                        constraintSet.setDimensionRatio(imageView.getId(), itemProportion);
                    }
                }
            }
        }
        if (item.getPathType() == -1) {
            imageView.setImageDrawable(addDrawable);
//            if (Integer.parseInt(item.getPath()) == 0) {
//                imageView.setImageResource(R.drawable.gylibrary_add);
//            } else {
//                imageView.setImageResource(addRsId);
//            }
            deleteImg.setVisibility(View.GONE);
        } else {
            if (item.isEdit()) {
                if (deleteDrawable != null) {
                    deleteImg.setVisibility(View.VISIBLE);
                    deleteImg.getImageBuilder().setImageDrawable(deleteDrawable).build()
                            .getViewSet()
//                            .setMarginPx(0, deleteDrawableMarginTopPx, deleteDrawableMarginRightPx, 0)
                            .setSizePx(deleteWidthPx, deleteHeightPx);
                    ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) deleteImg.getLayoutParams();
                    layoutParams.setMargins(0, deleteDrawableMarginTopPx, deleteDrawableMarginRightPx, 0);
                    deleteImg.setLayoutParams(layoutParams);
                }
            } else {
                deleteImg.setVisibility(View.GONE);
            }
            switch (item.getPathType()) {
                case PathType.LOCAL:
                    imageView.setImageBitmap(getLocalBitmap(item.getPath()));
                    break;
                case PathType.NETWORK:
                    GYPictureDownload.with(imageView.getContext()).load(item.getPath()).into(imageView);
                    break;
                case PathType.RESOURCE:
                    imageView.setImageResource(Integer.parseInt(item.getPath()));
                    break;
            }
        }
    }

    /**
     * 加载本地图片
     *
     * @param url
     * @return
     */
    public static Bitmap getLocalBitmap(String url) {
        try {
            FileInputStream fis = new FileInputStream(url);
            return BitmapFactory.decodeStream(fis);  ///把流转化为Bitmap图片

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }
    }
}
